/**
 * ReportGear(2011)
 */
package com.reportgear.core.data;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;

import com.reportgear.core.data.model.SalaryDataObject;
import com.reportgear.core.xml.Dom4jUtils;
import com.sot.core.util.StringUtils;

/**
 * 抽象薪酬数据查询XML
 * 
 * @version 1.0 2011-5-28
 * @author <a herf="lexloo@gmail.com">lexloo</a>
 * @since Report 1.0
 */
public abstract class AbstractSalaryDataQueryXml implements SalaryDataQueryDelegate {
	/**
	 * XML数据转换为List
	 * 
	 * @param xml
	 *            xml数据字符串
	 */
	protected SalaryDataObject xmlRootToList(String xml) {
		SalaryDataObject sdo = new SalaryDataObject();

		if (StringUtils.isEmpty(xml)) {

		} else {
			Document doc = Dom4jUtils.createDocument(xml);
			Element root = doc.getRootElement();

			for (Object ao : root.attributes()) {
				Attribute aa = (Attribute) ao;

				sdo.setValue(aa.getName(), StringUtils.trim(aa.getValue()));
			}
		}

		return sdo;
	}

	/**
	 * XML数据转换为List
	 * 
	 * @param xml
	 *            xml数据字符串
	 */
	protected List<SalaryDataObject> xmlDataToList(String xml) {
		if (StringUtils.isEmpty(xml)) {
			return Collections.<SalaryDataObject> emptyList();
		} else {
			List<SalaryDataObject> values = new ArrayList<SalaryDataObject>();

			Document doc = Dom4jUtils.createDocument(xml);
			Element root = doc.getRootElement();

			for (Object o : root.elements()) {
				Element el = (Element) o;

				SalaryDataObject sdo = new SalaryDataObject();
				for (Object ao : el.attributes()) {
					Attribute aa = (Attribute) ao;

					sdo.setValue(aa.getName(), StringUtils.trim(aa.getValue()));
				}

				values.add(sdo);
			}

			return values;
		}
	}
}
